"use strict"; + function ($, window, undefined) {
    $.extend($.fn, {
        vSelect: function (options, form) {

            if (!$("head style[_ui]").length) {
                var styleHtml = "<style _ui='lock'>" +
                    ".has-error .select_current{color: rgb(246, 86, 98)!important}" +
                    /*  ".has-success{border: 1px solid rgb(106, 196, 81)!important;} " +*/
                    ".has-success .select_current{color: #999}" +
                    "</style>";

                $("head").append(styleHtml);
            }

            var selector = this.selector;

            var config = {};

            $.extend(true, config, options);

            $(this).each(function (index, element) {

                var self = $(element);

                if (self.parent(".select_wrap").length > 0) {
                    return false;
                }

                var parentHash = "jQuery_" + new Date().getTime();

                self.wrap('<span class="select_wrap" _select=' + parentHash + '></span>');

                $('<span class="select_current"></span>' + '<span class="select_list"><dl></dl></span>' + '<span class="select_btn"></span>').insertBefore(self);

                var _parentId = "[_select='" + parentHash + "']";
                var _lists = _parentId + " .select_list";
                var _current = _parentId + " .select_current";
                var _arrow = _parentId + " .select_btn";
                var _list = _lists + " dl dd";
                var _selected = _parentId + " select option:selected";
                var _index = $(_selected).index();
                var _text = $(_selected).text();

                $(_current).text(_text);

                self.children().length && self.children().each(function (index, element) {

                    if ($.nodeName(element, "OPTION")) {
                        if (config.lp && !$(element).val()) {
                            $(_lists + " dl").append('<dd style="display: none;">' + $(element).text() + '</dd>');
                        } else {
                            $(_lists + " dl").append('<dd>' + $(element).text() + '</dd>');
                        }

                    } else if ($.nodeName(element, "OPTGROUP")) {
                        $(_lists + " dl").append('<dt>' + $(element).attr("label") + '</dt>');
                        $(element).children().length && $(element).children().each(function (index, element) {
                            $(_lists + " dl").append('<dd>' + $(element).text() + '</dd>');
                        });
                    }

                });

                /**
                 * [events bind]
                 */

                $(_current + ", " + _arrow).on("click", function (event) {

                    event.stopPropagation();

                    var _this = this;

                    $("[_select]").each(function (index, element) {
                        if ($(element).attr("_select") != parentHash) {
                            $(element).find(".select_btn").removeClass("select_btn_on");
                            $(element).find(".select_list").hide();
                        }
                    });

                    $(_parentId).removeClass("has-error");
                    $(_current).css("border", "");

                    var bh = $(window).height() - $(_parentId).offset().top;
                    var h = $(_lists).height();
                    var _top = $(_current).height();

                    if (h >= 350) {
                        h = -350;
                    } else {
                        h = -h;
                    }
                    if ($(_parentId).offset().top - $(window).scrollTop() < 350) {
                        $(_lists).css({
                            "top": _top
                        });
                    } else {
                        $(_list).css({
                            "top": h
                        });
                    }

                    $(_lists).stop().toggle();
                    $(_arrow).toggleClass("select_btn_on");

                    return false;

                });

                $(_list).each(function (index, element) {

                    $(element).on("click", function (event) {

                        event.stopPropagation();

                        $(_current).text($(this).text());
                        $(this).addClass("selected").siblings().removeClass("selected");

                        $(_lists).removeAttr("style");

                        $(_arrow).removeClass("select_btn_on");

                        // self.get(0).selectedIndex = index + 1;
                        self.get(0).selectedIndex = index;
                        self.trigger("change");

                        // no idea
                        $(_parentId + " .msg-box").empty();


                        if ($(this).text() != _text) {
                            $(_parentId).addClass("has-success");
                        } else {
                            $(_parentId).removeClass("has-success");
                        }

                        return false;

                    });

                });

                $(document).on("click", function (argument) {
                    $(_lists).stop().hide();
                    $(_arrow).removeClass("select_btn_on");
                });

                // for mobile
                if (navigator.userAgent.match(/mobile/i)) {

                    $(_list).off("click").hide();
                    $(_current).off("click");
                    $(_arrow).off("click");
                    $(_parentId).find("select").show().css({
                        "opacity": 0,
                        "z-index": 300,
                        "visibility": "visible"
                    }).change(function () {

                        var sel_val = $(this).find("option:selected").text();

                        $(_current).text(sel_val);

                        if (sel_val != _text) {
                            $(_parentId).addClass("has-success");
                        } else {
                            $(_parentId).removeClass("has-success");
                        }

                    });

                }

            });



        },
        vRadio: function (options, form) {

            var selector = this.selector;

            if (this.parent(".radio_wrap").length > 0) {
                return this;
            }

            this.wrap('<span class="radio_wrap"></span>'); //wrap the radio
            this.find("input").filter(":radio[checked]").parents(".radio_wrap").addClass("radio_on_wrap"); //add the class="radio_on_wrap" for selected option

            this.on("click", function () {
                $(form).find("input[type='radio']").removeAttr("checked");
                $(this).find("input[type='radio']").prop("checked", true);
                
                $(form).find(".radio_wrap").removeClass("radio_on_wrap");
                $(this).parent(".radio_wrap").addClass("radio_on_wrap");
            });
            return this;

        },
        vCheckBox: function (argument) {
            // body...
        }
    });

}(jQuery, window, undefined);